package com.wuwei.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.gulimall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuwei.common.utils.PageUtils;
import com.wuwei.common.utils.Query;

import com.wuwei.gulimall.product.dao.BrandDao;
import com.wuwei.gulimall.product.entity.BrandEntity;
import com.wuwei.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        //根据传来的key进行模糊查询,可模糊查询的条件有品牌名和介绍
       LambdaQueryWrapper<BrandEntity> queryWrapper = new LambdaQueryWrapper<>();
       if (StringUtils.isNotEmpty(key)){
           queryWrapper.eq(BrandEntity::getBrandId,key).or().like(BrandEntity::getName,key);
       }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 更新时必须保证其他表中的冗余字段也同步更新
     * @param brand
     */
    @Override
    @Transactional
    public void updateDetail(BrandEntity brand) {
        this.updateById(brand);
        if (StringUtils.isNotEmpty(brand.getName())){
            //更新关联表中brand的name
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
        }
        //TODO 更新其他关联
    }

}
